﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Motion;

namespace Kimd.DMCEtherCard
{
    public class DMCEtherCardCoordinateOperator : ICoordinate
    {
        private bool _isErr;
        private string _lastError;
        public bool IsError { get; set; }

        public string LastError { get; set; }

        public bool CheckApiResult(short rtn, string errMessage)
        {
            if (rtn != 0)
            {
                //_isApiErr = true;
                //string errorStr = ErrorCode2ErrorStr(rtn);
                //MachineMessage.CallException(ExceptionType.FatalErr, "轴卡", $"[{rtn}{errorStr}]:{err}", MachineErrCode.CardErr);
                return false;
            }
            return true;
        }

        public bool IsBufferDone()
        {
            throw new NotImplementedException();
        }

        public bool IsCoordinateDone(short cardNo)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_check_done((ushort)cardNo, 0);
            return CheckApiResult(rtn, "查询坐标系运动状态失败!");
        }

        public bool MoveArc(short cardNo, short axisNo1, short axisNo2, int[] distancesInPulse, int radiusInpulse, bool isCCW, AxisSpeed crdSpeed)
        {
            short rtn = 0;
            List<double> posList = new List<double>();
            foreach (int item in distancesInPulse)
            {
                posList.Add((double)item);
            }
            double[] posInPulse = posList.ToArray();
            ushort[] axisList = new ushort[2] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1) };
            rtn += csLTDMC.LTDMC.dmc_set_vector_profile_unit((ushort)cardNo, 0, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
            //圆弧方向， 0 ：顺时针 1 ：逆时针
            //运动模式， 0 ：相对坐标模式 1 ：绝对坐标模式
            rtn += csLTDMC.LTDMC.dmc_arc_move_radius_unit((ushort)cardNo, 0, 2, axisList, posInPulse, radiusInpulse, isCCW ? (ushort)1 : (ushort)0, 0, 1);
            return CheckApiResult(rtn, $"设置两轴圆弧插补失败!");
        }

        public bool MoveBufferLine(short cardNo, short axisNo1, short axisNo2, List<OneMoveSegment> points)
        {
            throw new NotImplementedException();
        }

        public bool MoveLine2(short cardNo, short crdNo, short axisNo1, short axisNo2, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_set_vector_profile_multicoor((ushort)cardNo, (ushort)crdNo, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
            rtn += csLTDMC.LTDMC.dmc_set_vector_s_profile((ushort)cardNo, (ushort)crdNo, 0, crdSpeed.STime);
            ushort[] axisList = new ushort[2] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1) };
            List<double> posList = new List<double>();
            foreach (int item in distancesInPulse)
            {
                posList.Add((double)item);
            }
            double[] posInPulse = posList.ToArray();
            if (moveType == MoveType.Absolute)
                csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, (ushort)crdNo, 2, axisList, posInPulse, 1);
            else
                csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, (ushort)crdNo, 2, axisList, posInPulse, 0);//运动模式，0：相对坐标模式，1：绝对坐标模式
            //return CheckApiResult(rtn, "设置两轴直线插补失败!");
            return true;
        }

        public bool MoveLine3(short cardNo, short axisNo1, short axisNo2, short axisNo3, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        {
            short rtn = 0;
            List<double> posList = new List<double>();
            foreach (int item in distancesInPulse)
            {
                posList.Add((double)item);
            }
            double[] posInPulse = posList.ToArray();
            rtn += csLTDMC.LTDMC.dmc_set_vector_profile_unit((ushort)cardNo, 0, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
            ushort[] axisList = new ushort[3] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1), (ushort)(axisNo3 - 1) };
            //运动模式，0：相对坐标模式，1：绝对坐标模式
            if (moveType == MoveType.Absolute)
                rtn += csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, 0, 3, axisList, posInPulse, 1);
            else
                rtn += csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, 0, 3, axisList, posInPulse, 0);
            return CheckApiResult(rtn, "设置三轴直线插补失败!");
        }

        //public bool MoveLine4(short cardNo, short axisNo1, short axisNo2, short axisNo3, short axisNo4, int[] distancesInPulse, AxisSpeed crdSpeed, MoveType moveType = MoveType.Absolute)
        //{
        //    short rtn = 0;
        //    List<double> posList = new List<double>();
        //    foreach (int item in distancesInPulse)
        //    {
        //        posList.Add((double)item);
        //    }
        //    double[] posInPulse = posList.ToArray();
        //    rtn += csLTDMC.LTDMC.dmc_set_vector_profile_unit((ushort)cardNo, 0, crdSpeed.VelStart, crdSpeed.Vel, crdSpeed.Acc, crdSpeed.Dec, crdSpeed.VelEnd);
        //    ushort[] axisList = new ushort[3] { (ushort)(axisNo1 - 1), (ushort)(axisNo2 - 1), (ushort)(axisNo3 - 1) };
        //    //运动模式，0：相对坐标模式，1：绝对坐标模式
        //    if (moveType == MoveType.Absolute)
        //        rtn += csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, 0, 3, axisList, posInPulse, 1);
        //    else
        //        rtn += csLTDMC.LTDMC.dmc_line_unit((ushort)cardNo, 0, 3, axisList, posInPulse, 0);
        //    return CheckApiResult(rtn, "设置三轴直线插补失败!");
        //}

        public void StopCrd(short CardNo, bool now)
        {
            short rtn = 0;
            rtn += csLTDMC.LTDMC.dmc_stop((ushort)CardNo, 0, 0);
        }
    }
}
